package atom.core2.j2ee.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;

import atom.core0.app.thread.manager.DaemonThreadManager;
import atom.core0.global.GlobalCache;
import atom.core0.global.GlobalCacheConst;
import atom.core0.util.IPUtil;
import atom.core0.util.StringUtil;
import atom.core2.j2ee.global.init.GlobalCacheInit;
import atom.core2.j2ee.util.ServletUtil;

/**
 * 初始化参数servlet.
 * 
 * @author aps-zmc
 * 
 */
public class InitGlobalInfoServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	private static Logger log = Logger.getLogger(InitGlobalInfoServlet.class);
	public static String encodingChar="GBK";

	public static void reinit() 
	{
		try
		{
			GlobalCacheInit.loadProperties(GlobalCacheConst.TYPE_SYSTEM,"/conf/global-info.bean",encodingChar);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
	}
	public void init() throws ServletException
	{
		super.init();

		ServletUtil.setServletContext(getServletContext());
		
		initIP();
		
		try
		{
			String charset = getInitParameter("encodingChar");
			if (!StringUtil.isEmpty(charset))
				encodingChar=charset;
			GlobalCacheInit.loadProperties(GlobalCacheConst.TYPE_SYSTEM,"/conf/global-info.bean",encodingChar);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}



	}

	public void destroy()
	{
		DaemonThreadManager.interrupt();
	}
	public static void initIP()
	{
		// 初始化ip地址
		String ip = "";
		try
		{
			ip = IPUtil.getLocalIPV4();// 很费时间 1000次2秒 记录日志用
			System.setProperty("ip", ip);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		GlobalCache.setCache(GlobalCacheConst.TYPE_SYSTEM, GlobalCacheConst.KEY_IP, ip);

	}

	
}
